#include "../include/Customer.hh"
#include "../include/SavingsAccount.hh"
#include "../include/CheckingAccount.hh"
#include <iostream>
#include <algorithm>
#include <iomanip>

Customer::Customer(std::string name)
    : User(name, Role::CUSTOMER), accountPassword("000000") { // 默认密码为000000
}

Customer::Customer(std::string name, std::string password)
    : User(name, Role::CUSTOMER), accountPassword(password) {
}

void Customer::addAccount(std::shared_ptr<Account> account) {
    // 确保添加的账户属于该客户
    if (account && account->getOwnerName() == getUsername()) {
        accounts.push_back(account);
    }
}

std::shared_ptr<Account> Customer::getAccountById(const std::string& accountId) const {
    auto it = std::find_if(accounts.begin(), accounts.end(),
                           [&accountId](const std::shared_ptr<Account>& acc) {
                               return acc->getAccountId() == accountId;
                           });
    
    if (it != accounts.end()) {
        return *it;
    }
    
    return nullptr; // 没有找到匹配的账户
}

const std::vector<std::shared_ptr<Account>>& Customer::getAccounts() const {
    return accounts;
}

std::shared_ptr<Account> Customer::getSavingsAccount() const {
    auto it = std::find_if(accounts.begin(), accounts.end(),
                          [](const std::shared_ptr<Account>& acc) {
                              return dynamic_cast<SavingsAccount*>(acc.get()) != nullptr;
                          });
    
    if (it != accounts.end()) {
        return *it;
    }
    
    return nullptr; // 没有找到储蓄账户
}

std::shared_ptr<Account> Customer::getCheckingAccount() const {
    auto it = std::find_if(accounts.begin(), accounts.end(),
                          [](const std::shared_ptr<Account>& acc) {
                              return dynamic_cast<CheckingAccount*>(acc.get()) != nullptr;
                          });
    
    if (it != accounts.end()) {
        return *it;
    }
    
    return nullptr; // 没有找到支票账户
}

void Customer::displayInfo() const {
    std::cout << "===== 客户信息 =====" << std::endl;
    std::cout << "姓名: " << getUsername() << std::endl;
    
    std::cout << std::endl << "账户列表:" << std::endl;
    
    if (accounts.empty()) {
        std::cout << "该客户还没有开设任何账户。" << std::endl;
    } else {
        int i = 1;
        for (const auto& account : accounts) {
            std::cout << i++ << ". ";
            if (dynamic_cast<SavingsAccount*>(account.get())) {
                std::cout << "储蓄账户: ";
            } else if (dynamic_cast<CheckingAccount*>(account.get())) {
                std::cout << "支票账户: ";
            } else {
                std::cout << "未知类型账户: ";
            }
            
            std::cout << account->getAccountId() << "  |  账户类型: " << account->getAccountTypeName() << std::endl;
            std::cout << "  人民币余额: ¥" << std::fixed << std::setprecision(2) << account->getBalance(Currency::CNY) << std::endl;
            std::cout << "  美元余额: $" << std::fixed << std::setprecision(2) << account->getBalance(Currency::USD) << std::endl;
            std::cout << "  欧元余额: €" << std::fixed << std::setprecision(2) << account->getBalance(Currency::EUR) << std::endl;
        }
    }
}

// 验证账户密码
bool Customer::verifyPassword(const std::string& password) const {
    return accountPassword == password;
}

// 修改密码
bool Customer::changePassword(const std::string& oldPassword, const std::string& newPassword) {
    // 先验证旧密码是否正确
    if (!verifyPassword(oldPassword)) {
        std::cout << "错误：原密码不正确！" << std::endl;
        return false;
    }
    
    // 检查新密码是否为6位数字
    if (newPassword.length() != 6 || !std::all_of(newPassword.begin(), newPassword.end(), ::isdigit)) {
        std::cout << "错误：新密码必须为6位数字！" << std::endl;
        return false;
    }
    
    // 更新密码
    accountPassword = newPassword;
    std::cout << "密码修改成功！" << std::endl;
    return true;
}

// 账户删除确认
bool Customer::confirmDeleteAccount(const std::string& accountId, const std::string& password) const {
    // 先验证密码
    if (!verifyPassword(password)) {
        std::cout << "错误：密码不正确，无法确认删除操作！" << std::endl;
        return false;
    }
    
    // 查找账户
    auto account = getAccountById(accountId);
    if (!account) {
        std::cout << "错误：账户 " << accountId << " 不存在！" << std::endl;
        return false;
    }
    
    // 检查所有货币余额
    bool hasBalance = false;
    
    // 检查人民币余额
    if (account->getBalance(Currency::CNY) > 0.01) {
        std::cout << "错误：账户 " << accountId << " 仍有人民币余额 ¥" 
                  << std::fixed << std::setprecision(2) << account->getBalance(Currency::CNY) 
                  << "，请先将余额转出或取出！" << std::endl;
        hasBalance = true;
    }
    
    // 检查美元余额
    if (account->getBalance(Currency::USD) > 0.01) {
        std::cout << "错误：账户 " << accountId << " 仍有美元余额 $" 
                  << std::fixed << std::setprecision(2) << account->getBalance(Currency::USD) 
                  << "，请先将余额转出或取出！" << std::endl;
        hasBalance = true;
    }
    
    // 检查欧元余额
    if (account->getBalance(Currency::EUR) > 0.01) {
        std::cout << "错误：账户 " << accountId << " 仍有欧元余额 €" 
                  << std::fixed << std::setprecision(2) << account->getBalance(Currency::EUR) 
                  << "，请先将余额转出或取出！" << std::endl;
        hasBalance = true;
    }
    
    if (hasBalance) {
        return false;
    }
    
    return true; // 密码正确，且账户无余额，可以删除
} 